package com.test.controller;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.test.R;
import com.test.adapter.ExpandableListViewAdapter;
import com.test.entity.Group;
import com.test.entity.Item;
import com.test.util.StringUtil;

import java.util.ArrayList;

/**
 * 折叠样式控制层
 *
 * @Author Musique
 * 2020/8/6
 */
public class ExpandableListViewActivity extends AppCompatActivity {

    private Context mContext;

    private ExpandableListViewAdapter expandableListViewAdapter;

    private ExpandableListView tv_item_list;

    ArrayList<ArrayList<Item>> iconLists = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expandable_list_view);//把该类放在容器中

        mContext = this;
        tv_item_list = findViewById(R.id.elv_name);
        ArrayList<Group> groupList = new ArrayList<>();
        // Group group = new Group(); //这相当于一个对象
        for (int i = 1; i <= 4; i++) {
            Group group = new Group(); //这相当于4个对象
            group.setGroupName(StringUtil.itemSelect(i));
            groupList.add(group);
        }

        // 重点：数组长度为4，因此数组里面只可放入4组数据。
        // 多放无用，少放运行不会报错，但如果点击就会报错
        // 做个小测试，在adc多放 （多放2行数据）
        // 原因，因为数组长度为4（以固定数组长度，再往里面加数据会无效，具体看java基础，**数组**）

        // adc
        ArrayList<Item> iconList = new ArrayList<>();
        iconList.add(new Item(R.mipmap.iv_lol_icon3, "剑圣"));
        iconList.add(new Item(R.mipmap.iv_lol_icon4, "德莱文"));
        iconList.add(new Item(R.mipmap.iv_lol_icon13, "男枪"));
        iconList.add(new Item(R.mipmap.iv_lol_icon14, "韦鲁斯"));
        iconList.add(new Item(R.mipmap.iv_lol_icon8, "天使"));
        iconList.add(new Item(R.mipmap.iv_lol_icon9, "泽拉斯"));
        iconLists.add(iconList);
        //AP组
        iconList = new ArrayList<Item>();
        iconList.add(new Item(R.mipmap.iv_lol_icon1, "提莫"));
        iconList.add(new Item(R.mipmap.iv_lol_icon7, "安妮"));
        iconList.add(new Item(R.mipmap.iv_lol_icon8, "天使"));
        iconList.add(new Item(R.mipmap.iv_lol_icon9, "泽拉斯"));
        iconLists.add(iconList);
        //辅助
        iconList = new ArrayList<Item>();
        iconList.add(new Item(R.mipmap.iv_lol_icon2, "诺手"));
        iconList.add(new Item(R.mipmap.iv_lol_icon5, "德邦"));
        iconList.add(new Item(R.mipmap.iv_lol_icon6, "奥拉夫"));
        iconList.add(new Item(R.mipmap.iv_lol_icon10, "龙女"));
        iconLists.add(iconList);
        //TANK组
        iconList = new ArrayList<Item>();
        iconList.add(new Item(R.mipmap.iv_lol_icon2, "诺手"));
        iconList.add(new Item(R.mipmap.iv_lol_icon5, "德邦"));
        iconList.add(new Item(R.mipmap.iv_lol_icon6, "奥拉夫"));
        iconList.add(new Item(R.mipmap.iv_lol_icon10, "龙女"));
        iconList.add(new Item(R.mipmap.iv_lol_icon12, "狗熊"));
        iconLists.add(iconList);

        expandableListViewAdapter = new ExpandableListViewAdapter(groupList, iconLists, mContext);
        tv_item_list.setAdapter(expandableListViewAdapter);

        // 列表点击事件
        tv_item_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                Toast.makeText(mContext, "你点击了：" + iconLists.get(i).get(i1).getTitle(), Toast.LENGTH_LONG).show();
                return true;
            }
        });
    }

    public static void main(String[] args) {
        ArrayList<Group> groupList = new ArrayList<>();
        for (int i = 1; i <= 4; i++) {
            Group group = new Group();// 这创建了几个对象 ？？
            group.setGroupName(StringUtil.itemSelect(i));
            groupList.add(group);
        }
        System.out.println(groupList);
    }
}